using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Turret : MonoBehaviour {
    public float fireSpeed = 2.0f;  //how frequently the turret fires a laser
    public GameObject laser;        //what GameObject will be fired from the turret
    public enum FACING { NORTH, EAST, SOUTH, WEST };
    public FACING startingDirection;
    public enum ROTATION { CLOCKWISE, COUNTERCLOCKWISE, NOPE };
    public ROTATION rotateScheme = ROTATION.NOPE;

    private FACING currentDirection;
    private float lamda;
    private Dictionary<FACING, float> rotations;
    private int mouthIndex = -1;

	// Use this for initialization
	void Start () {
        TurretManager.Register(gameObject);
        currentDirection = startingDirection;
        rotations = new Dictionary<FACING, float>();
        rotations[FACING.NORTH] = 0;
        rotations[FACING.EAST] = 90;
        rotations[FACING.SOUTH] = 180;
        rotations[FACING.WEST] = 270;
        transform.localEulerAngles = new Vector3(0, rotations[currentDirection], 0);
        for (int i = 0; i < transform.GetChildCount(); ++i)
            if (transform.GetChild(i).name == "Mouth")
            {
                mouthIndex = i;
                break;
            }
        /*
       Color temp = new Color();
       switch (rotateScheme)
       {
           case ROTATION.CLOCKWISE:
               temp = Color.red;
               break;
           case ROTATION.COUNTERCLOCKWISE:
               temp = Color.blue;
               break;
       }
       for (int i = 0; i < transform.GetChildCount(); ++i)
           if (transform.GetChild(i).name == "helpmeee")
           {
               Renderer []derp = transform.GetChild(i).GetComponentsInChildren<Renderer>();
               foreach (Renderer r in derp)
                   r.sharedMaterial.SetColor("_Color", temp);
               break;
           }
        */
	}

    // Update is called once per frame
    void Update()
    {
        switch (rotateScheme)
        {
            case ROTATION.CLOCKWISE:
                gameObject.transform.localEulerAngles = new Vector3(0,
                    Mathf.Lerp(rotations[currentDirection], rotations[currentDirection] + 90, lamda),
                    0);
                break;
            case ROTATION.COUNTERCLOCKWISE:
                gameObject.transform.localEulerAngles = new Vector3(0,
                    Mathf.Lerp(rotations[currentDirection], rotations[currentDirection] - 90, lamda),
                    0);
                break;
        }
	}

    public void SetRotationTimer(float t)
    {
        lamda = t;
    }

    public void Fire()
    {
        Vector3 mouth = gameObject.transform.position;
        if (mouthIndex > -1)
             mouth = gameObject.transform.GetChild(mouthIndex).transform.position;

        GameObject newLaser = (GameObject)GameObject.Instantiate(laser, mouth, gameObject.transform.rotation);
        newLaser.AddComponent<Laser>();
        newLaser.GetComponent<Laser>().SetVelocity(0, fireSpeed, 0);
        newLaser.GetComponent<Laser>().CanAge(true);
        newLaser.GetComponent<Laser>().SetTurretName(gameObject.name);
        newLaser.transform.Rotate(90, 0, 0);
        
        switch (rotateScheme)
        {
            case ROTATION.CLOCKWISE:
                if (FACING.WEST == currentDirection)
                    currentDirection = FACING.NORTH;
                else
                    ++currentDirection;
                break;
            case ROTATION.COUNTERCLOCKWISE:
                if (FACING.NORTH == currentDirection)
                    currentDirection = FACING.WEST;
                else
                    --currentDirection;
                break;
        }
        lamda = 0;
        gameObject.transform.localEulerAngles = new Vector3(0, rotations[currentDirection], 0);
        Debug.Log("Turret::Fire()");
    }
}
